package com.wsh.crawler.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class CrawlerExecutorConfig {

    /**
     * 文章级并发执行线程池（受管，便于限流与关闭）
     * 2C/2G/3M 不能太高并发，避免网络打满
     */
    // 关键：返回类型改为 ThreadPoolTaskExecutor
    @Bean("articleExecutor")
    public ThreadPoolTaskExecutor articleExecutor() {
        ThreadPoolTaskExecutor exec = new ThreadPoolTaskExecutor();
        exec.setCorePoolSize(3);
        exec.setMaxPoolSize(4);
        exec.setQueueCapacity(100);
        exec.setThreadNamePrefix("article-");
        exec.setKeepAliveSeconds(60);
        exec.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        exec.setWaitForTasksToCompleteOnShutdown(true);
        exec.setAwaitTerminationSeconds(10);
        // 不需要手动 initialize()，Spring 会调用
        return exec;
    }
}